class SoftwareIdeaController extends BaseController {
    
	def beforeInterceptor = [action:this.&auth, except:['list']]
	
    def index = { redirect(action:list, params:params) }

    // the delete, save and update actions only
    // accept POST requests
    def allowedMethods = [delete:'POST',
                          save:'POST',
                          update:'POST']

    def list = {
		params.max = 10
		params.order = 'desc'
        [ideaList:SoftwareIdea.listOrderByCreatedOn(params), total:SoftwareIdea.count()]
    }

	def create = {
    }

    def save = {
        //TODO: ??add functionality for a list of %like% ideas with a separate "like ideas" list view
		//and 'confirmSave' and 'scratch' actions??
		def idea = SoftwareIdea.findByAbout(params.about.trim())
		if(idea) {
			flash.message = 'Exactly the same idea already exists'
			render(view:'show', model:[softwareIdea:idea])
			return
		}
		else {
			idea = new SoftwareIdea()
        	idea.properties = params
			idea.permalink = params.about.encodeAsPermalink()
			idea.byUser = session.user
			//Add tags if any
			def tags = params.tagTokens.encodeAsUniqueList()
			tags.each {
				def t = Tag.findByName(it)
				if(!t) t = new Tag(name:it)
				idea.addToTags(t)
			}
			if(idea.save()) {
           		render(view:'show', model:[softwareIdea:idea])
        	}
        	else {
            	render(view:'create', model:[softwareIdea:idea])
        	}
    	}
	}
	
	def show = {
		def idea = SoftwareIdea.findByPermalink(params.permalink)
		[softwareIdea:idea]
    }
}